package com.hanter.android.saf.result;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;

import androidx.activity.result.IntentSenderRequest;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class StartIntentSenderForResult
        extends ActivityResultContract<IntentSenderRequest, IntentSenderRequestResult> {

    /**
     * An {@link Intent} action for making a request via the
     * {@link Activity#startIntentSenderForResult} API.
     */
    public static final String ACTION_INTENT_SENDER_REQUEST = "androidx.activity.result"
            + ".contract.action.INTENT_SENDER_REQUEST";

    /**
     * Key for the extra containing the {@link IntentSenderRequest}.
     *
     * @see #ACTION_INTENT_SENDER_REQUEST
     */
    public static final String EXTRA_INTENT_SENDER_REQUEST = "androidx.activity.result"
            + ".contract.extra.INTENT_SENDER_REQUEST";

    /**
     * Key for the extra containing the {@link android.content.IntentSender.SendIntentException}
     * if the call to
     * {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int)}
     * fails.
     */
    public static final String EXTRA_SEND_INTENT_EXCEPTION = "androidx.activity.result"
            + ".contract.extra.SEND_INTENT_EXCEPTION";

    private IntentSenderRequest input;

    @NonNull
    @Override
    public Intent createIntent(@NonNull Context context, @NonNull IntentSenderRequest input) {
        this.input = input;
        return new Intent(ACTION_INTENT_SENDER_REQUEST)
                .putExtra(EXTRA_INTENT_SENDER_REQUEST, input);
    }

    @NonNull
    @Override
    public IntentSenderRequestResult parseResult(
            int resultCode, @Nullable Intent intent) {
        return new IntentSenderRequestResult(input, resultCode, intent);
    }
}